﻿using Coffee_Restaurant.Database;
using System;
using System.Linq;
using System.Windows.Forms;

namespace Coffee_Restaurant.Forms.DinnerMenus
{
    public partial class DinnerMenuAddEdit : Form
    {
        //event edit
        public delegate void ButtonSaveEventHandler(string name);
        public event ButtonSaveEventHandler BtnAdd;

        //event save
        public delegate void ButtonSaveEditEventHandler();
        public event ButtonSaveEditEventHandler BtnEdit;

        //idEdit
        public int ItemIdSelect { get; set; }

        public int? ItemCategoryId { get; set; }

        public DinnerMenuAddEdit()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        public bool ValidateForm()
        {
            //validate
            if (string.IsNullOrEmpty(txtName.Text) || string.IsNullOrWhiteSpace(txtName.Text))
            {
                txtName.BackColor = System.Drawing.Color.Khaki;
                txtName.Focus();
                return false;
            }
            txtName.BackColor = System.Drawing.Color.White;

            if (string.IsNullOrEmpty(txtPrice.Text) || string.IsNullOrWhiteSpace(txtPrice.Text))
            {
                txtPrice.BackColor = System.Drawing.Color.Khaki;
                txtPrice.Focus();
                return false;
            }
            txtPrice.BackColor = System.Drawing.Color.White;

            if (string.IsNullOrEmpty(cbbItemUnits.Text) || string.IsNullOrWhiteSpace(cbbItemUnits.Text))
            {
                cbbItemUnits.BackColor = System.Drawing.Color.Khaki;
                cbbItemUnits.Focus();
                return false;
            }
            cbbItemUnits.BackColor = System.Drawing.Color.White;
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (btnSave.Name == "btnSaveEdit")
            {
                if (BtnEdit != null && ValidateForm() && ItemIdSelect > 0)
                {
                    try
                    {
                        using (var db = new DatabaseEntities())
                        {
                            //save to database
                            var model = db.Items.FirstOrDefault(x => x.ItemId == ItemIdSelect);
                            if (model != null)
                            {
                                model.ItemCategoriesId = int.Parse(cbbItemCategory.SelectedValue.ToString());
                                model.Name = txtName.Text;
                                model.Price = Decimal.Parse(txtPrice.Text);
                                model.Unit = cbbItemUnits.Text;
                                model.InWarehouse = 0;
                            }
                            if (db.SaveChanges() > 0)
                            {
                                if (model != null) BtnEdit();
                                this.Hide();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        BtnEdit();
                    }
                }
            }
            else
            {
                if (BtnAdd != null && ValidateForm())
                {
                    try
                    {
                        using (var db = new DatabaseEntities())
                        {

                            //add to database
                            var guid = Guid.NewGuid().ToString();
                            db.Items.Add(new Item()
                            {
                                ItemCategoriesId = int.Parse(cbbItemCategory.SelectedValue.ToString()),
                                Name = txtName.Text,
                                Unit = cbbItemUnits.Text,
                                Price = decimal.Parse(txtPrice.Text),
                                Date = DateTime.Now,
                                GuiId = guid
                            });
                            if (db.SaveChanges() > 0)
                            {
                                var model = db.Items.FirstOrDefault(a => a.GuiId == guid);
                                if (model != null)
                                {
                                    BtnAdd(model.Name);
                                }
                                this.Hide();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        BtnAdd(null);
                    }
                }
            }
        }


        //esc to exit
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void ItemAddEdit_Load(object sender, EventArgs e)
        {
            using (var db = new DatabaseEntities())
            {
                //load nhom san pham
                var model = db.ItemCategories.ToList();
                cbbItemCategory.DataSource = model;
                cbbItemCategory.DisplayMember = "Name"; //ten truong hien thi
                cbbItemCategory.ValueMember = "ItemCategoriesId"; //ten cot gia tri
                cbbItemCategory.SelectedValue = ItemCategoryId; //default select when click treeview


                //load units
                var modelUnits = db.ItemUnits.ToList();
                cbbItemUnits.DataSource = modelUnits;
                cbbItemUnits.DisplayMember = "Name";
                cbbItemUnits.ValueMember = "Name";
            }
            foreach (Button control in Controls.OfType<Button>())
            {
                if (control.Name != "btnClose")
                {
                    control.MouseHover += btn_MouseHover;
                    control.MouseLeave += btn_MouseLeave;
                }
            }
        }


        private void btn_MouseHover(object sender, EventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                btn.BackgroundImage = global::Coffee_Restaurant.Properties.Resources.woodbutton_hover;
            }
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                btn.BackgroundImage = global::Coffee_Restaurant.Properties.Resources.woodbutton;
            }
        }

    }
}
